package com.topone.service.impl;

import com.topone.Enum.ResultEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.UserForm;
import com.topone.Utils.ServiceUtils;
import com.topone.dataObject.Mapper.UserMapper;
import com.topone.dataObject.TcUser;
import com.topone.dto.TcUserDTO;
import com.topone.repository.TcUserRepository;
import com.topone.service.TcUserService;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * Created by Administrator on 2017/11/6,0006.
 */
@Service
public class TcUserServiceImpl implements TcUserService {

    @Autowired
    private TcUserRepository repository;
    @Autowired
    private UserMapper userMapper;

    public TcUser findOne(String userId) {
        TcUser tcUser = new TcUser();
        if (!("".equals(userId)) && userId.isEmpty()) {
            tcUser = repository.getOne(userId);
        }
        return tcUser;
    }

    public TcUser findUserInfo(String userName) {
        TcUser tcUser = new TcUser();
        tcUser = repository.findByUserName(userName);
        return tcUser;
    }

    @Override
    public TcUser save(TcUser tcUser) {
        return repository.save(tcUser);
    }

    @Override
    public TcUser findByopenid(String openid) {
        return repository.findByOpenid(openid);
    }

    @Override
    public TcUserDTO findUserInfoByopenid(String openid) {
        TcUserDTO tcUser = new TcUserDTO();
        try {
            tcUser = userMapper.findUserInfo(openid);
        } catch (FanZoneException e) {
            throw new FanZoneException(ResultEnum.USER_ERROR);
        }
        return tcUser;
    }

    @Override
    public Integer insertUserInfo(TcUserDTO tcUserDTO) {
        return userMapper.insertUserInfo(tcUserDTO);
    }

    @Override
    public Integer updateUserInfo(TcUserDTO tcUserDTO) {
        return userMapper.updateUserInfo(tcUserDTO);
    }

    @Override
    public TcUserDTO findUserByOpenid(String openid) {
        return userMapper.findUserInfoByOpenid(openid);
    }

    @Override
    public TcUser findByPhone(String phone) {
        return repository.findByHandPhone(phone);
    }

    @Override
    public TcUser saveUser(WxMpUser wxMpUser) {
        TcUser savedTcUser = null;
        if (Objects.nonNull(wxMpUser) && StringUtils.isNotEmpty(wxMpUser.getOpenId())) {
            savedTcUser = findByopenid(wxMpUser.getOpenId());
        }
        TcUser toUser = ServiceUtils.toWechat(wxMpUser);
        if (Objects.nonNull(savedTcUser)) {
            toUser.setUserId(savedTcUser.getUserId());
            toUser.setHandPhone(savedTcUser.getHandPhone());
            return save(toUser);
        } else {
            toUser.setUserId(UUID.randomUUID().toString().replace("-", ""));
            return save(toUser);
        }
    }

    @Override
    public List<TcUser> findUsers(UserForm userForm) {
        return repository.findAll();
    }
}
